import { defineStore } from 'pinia'
import homeApi from '@/api/home'
import type { reqSearchInfoParamsModel, goodsListModel } from '@/api/home'
import { showToast } from 'vant';

const searchList = () => ({
    category3Id: "", // 三级分类
    categoryName: "", // 三级分类
    keyword: "", // 搜索
    trademark: "", // 品牌  "4:小米"   `品牌id:品牌名称`  `tmId:tmName`
    props: [], // 平台属性 ["1:1700-2799:价格"] [`平台属性id:平台属性值:平台属性名`]  [`attrId:attrValue:attrName`]
    order: "1:desc", // 排序   1 综合 2 价格     desc 降序 asc 升序   默认`1:desc`是`综合:降序`
    pageNo: 1, // 当前页
    pageSize: 3, // 每页条数
})

let isChild = true
interface stateModel {
    params: reqSearchInfoParamsModel,
    goodsList: goodsListModel[],
    active: number
}

const useNumStore = defineStore('homeStore', {
    state: function(): stateModel {
        return {
            params: searchList(),
            goodsList: [],
            active: 0
        }
    },
    actions: {
        async getGoodsList() {
            try {
                if(isChild) {
                    let result = await homeApi.reqSearchInfo(this.params)
                    if(result.goodsList.length < this.params.pageSize) {
                        isChild = false
                        return
                    } else {
                        isChild = true
                    }
                    this.goodsList.push(...result.goodsList)
                    this.params.pageNo++
                    
                } else{
                    console.log('已经没有数据了');
                    showToast({
                        message: '我是有底线的',
                    })
                }
            } catch (error) {
                console.log(error);
                showToast({
                    message: '数据请求失败',
                    icon:'fail'
                })
            }
        },
        setTabarNumber(e: number) {
            console.log('123',e);
            this.active = e
        }
    },
    getters: {}
})

export default useNumStore